#define _CRT_SECURE_NO_WARNINGS

#include "DList.h"

DL* BuyDL(DLtype x)
{
	DL* newnode = (DL*)malloc(sizeof(DL));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}

DL* DLInit()
{
	DL* head = BuyDL(-1);
	head->next = head;
	head->prev = head;
	return head;
}

void DLPushBack(DL* phead, DLtype x)
{
	assert(phead);
	DL* newnode = BuyDL(x);
	DL* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	tail = newnode;
	newnode->next = phead;
}

void DLPopBack(DL* phead)
{
	assert(phead && phead->next);
	DL* tail = phead->prev;
	DL* tmp = tail->prev;
	free(tail);
	tail = tmp;
	tmp->next = phead;
}

void DLPushFront(DL* phead, DLtype x)
{
	assert(phead);
	DL* newnode = BuyDL(x);
	DL* tmp = phead->next;
	newnode->next = tmp;
	tmp->prev = newnode;
	newnode->prev = phead;
	phead->next = newnode;
}

void DLPopFront(DL* phead)
{
	assert(phead && phead->next);
	DL* tmp = phead->next->next;
	free(phead->next);
	phead->next = tmp;
	tmp->prev = phead;
}
